package com.example.swimfish;

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.transform.Scale;
import javafx.util.Duration;

import java.util.UUID;

public class AnimatedFishModel extends Group {
    private static final double SWIM_SPEED = 1.0;
    private static final int FRAME_COUNT = 8;
    private static final int FRAME_WIDTH = 174;
    private static final int FRAME_HEIGHT = 126;
    private String fishId;
    private String ownerId;
    private final ImageView fishView;
    private final Timeline swimAnimation;
    private final Scale flipTransform = new Scale(1, 1);

    private double directionX = 1;
    private double directionY = 0;
    private int currentFrame = 0;

    public AnimatedFishModel( Image fishSpriteSheet) {
        this.fishId = UUID.randomUUID().toString();
        this.ownerId = ownerId;

        this.fishView = new ImageView(fishSpriteSheet);
        updateFrame();

        this.swimAnimation = new Timeline(
                new KeyFrame(Duration.millis(100), event -> {
                    currentFrame = (currentFrame + 1) % FRAME_COUNT;
                    updateFrame();
                })
        );
        this.swimAnimation.setCycleCount(Animation.INDEFINITE);

        this.getTransforms().add(flipTransform);
        this.getChildren().add(fishView);

        this.setScaleX(0.5);
        this.setScaleY(0.5);

        // 初始随机方向
        changeDirectionRandomly();

//        startSwimming(); // 自动开始游动
    }

    private void updateFrame() {
        fishView.setViewport(new Rectangle2D(
                0,
                currentFrame * FRAME_HEIGHT,
                FRAME_WIDTH,
                FRAME_HEIGHT
        ));
    }

    public void startSwimming() {
        swimAnimation.play();
    }

    public void stopSwimming() {
        swimAnimation.stop();
    }
    public void updatePosition() {
        double newX = this.getTranslateX() + directionX * SWIM_SPEED;
        double newY = this.getTranslateY() + directionY * SWIM_SPEED;

        this.setTranslateX(newX);
        this.setTranslateY(newY);

        // 根据方向翻转鱼
        if (directionX < 0) {
            flipTransform.setX(-1);
        } else {
            flipTransform.setX(1);
        }
    }
//    public void updatePosition(Aquarium2DView aquarium) {
//        double newX = this.getTranslateX() + directionX * SWIM_SPEED;
//        double newY = this.getTranslateY() + directionY * SWIM_SPEED;
//
////        System.out.printf("更新前位置: (%.1f, %.1f) 方向: (%.2f, %.2f)%n",
////                this.getTranslateX(), this.getTranslateY(), directionX, directionY);
//
//                // 边界检查
//        boolean changedDirection = false;
//        if (newX < 0) {
//            newX = 0;
//            directionX = -directionX;
//            changedDirection = true;
//        } else if (newX > aquarium.getWidth() - FRAME_WIDTH * 0.5) {
//            newX = aquarium.getWidth() - FRAME_WIDTH * 0.5;
//            directionX = -directionX;
//            changedDirection = true;
//        }
//
//        if (newY < 0) {
//            newY = 0;
//            directionY = -directionY;
//            changedDirection = true;
//        } else if (newY > aquarium.getHeight() - FRAME_HEIGHT * 0.5) {
//            newY = aquarium.getHeight() - FRAME_HEIGHT * 0.5;
//            directionY = -directionY;
//            changedDirection = true;
//        }
//
//        // 如果碰到边界，随机改变方向
//        if (changedDirection && Math.random() < 0.3) {
//            changeDirectionRandomly();
//        }
////        System.out.printf("更新后位置: (%.1f, %.1f)%n", newX, newY);
//
//        this.setTranslateX(newX);
//        this.setTranslateY(newY);
//
//        // 根据方向翻转鱼
//        if (directionX < 0) {
//            flipTransform.setX(-1);
//        } else {
//            flipTransform.setX(1);
//        }
//
//
//    }

    public void setDirection(double x, double y) {
        double length = Math.sqrt(x*x + y*y);
        if (length > 0) {
            this.directionX = x / length;
            this.directionY = y / length;
        }
    }

    public void changeDirectionRandomly() {
        double angle = Math.random() * Math.PI * 2;
        setDirection(Math.cos(angle), Math.sin(angle));
    }

    public double getDirectionX() {
        return directionX;
    }

    public double getDirectionY() {
        return directionY;
    }

    public String getFishId() {
        return fishId;
    }

    public String getOwnerId() {
        return ownerId;
    }


}